﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Microsoft.Reporting.WinForms;
/*using Iprojects.DataLayer;*/
using Iprojects.Data;
using System.Linq;
using System.Data.Objects;
using Iprojects.Report;
/*using DbLinq;
using DbLinq.Sqlite;*/
/*using Iprojects.Data;*/
namespace Iprojects.Forms
{
    public partial class ReportPhoneByFamily : Form
    {
       
        AutoCompleteStringCollection FamilyCollection = new AutoCompleteStringCollection();
        AutoCompleteStringCollection StreetCollection = new AutoCompleteStringCollection();
      
        int familyid = 0;
        public ReportPhoneByFamily()
        {
            InitializeComponent();
            using (ZakahEntities db = new ZakahEntities())
            {
                
                foreach (var item in db.Persons.Take(1000))
	            {
                    FamilyCollection.Add(item.LastName);		
                   
	            }
                foreach (var item in db.FamilyPersons.Take(1000))
	            {
                    StreetCollection.Add(item.street);
                }
         
            
            }
            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = FamilyCollection;

            textStreet.AutoCompleteMode = AutoCompleteMode.Suggest;
            textStreet.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textStreet.AutoCompleteCustomSource = StreetCollection;

    
        //    dtGridFamilyProjectHistory.AutoGenerateColumns = false;

         //   dtGridFamilyProjectHistory.Columns.Add("sum", "مجموع  ");
          

            
           
        }
        void textBox1_Leave(object sender, System.EventArgs e)
        {
            btnAddFamily.Enabled = true;
        }
        void textStreet_Leave(object sender, System.EventArgs e)
        {
            btnAddFamily.Enabled = true;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            RenderReport(new moslemFamilyProject() );    

        }

      
        private void RenderReport(moslemFamilyProject moslemFamilyProject)
        {
            List<FamilyPerson> allFamiliesinProj=new List<FamilyPerson>();
            List<Person> allprj=new List<Person>();
            ZakahEntities db = new ZakahEntities();
           
            var query = from f in db.FamilyPersons.Where(x => x.street.StartsWith(textStreet.Text))
                        join p in db.Persons.Where(x => (x.LastName.StartsWith(textBox1.Text))) on f.id equals p.FamilyID
                        select new { f.street, p.FamilyID, p.IDnum, f.Husbandid, f.wifeid, f.cellnumber, f.Telephone, p.LastName, f.HomeNumber };
            if (!string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textStreet.Text))
            { 
            // search by both;
                query = from f in db.FamilyPersons
                        join p in db.Persons.Where(x => (x.LastName.StartsWith(textBox1.Text))) on f.id equals p.FamilyID
                        select new { f.street, p.FamilyID, p.IDnum, f.Husbandid, f.wifeid, f.cellnumber, f.Telephone, p.LastName, f.HomeNumber };
             
            }
            if (string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textStreet.Text))
            {
                // search by both;
                query = from f in db.FamilyPersons.Where(x => x.street.StartsWith(textStreet.Text))
                        join p in db.Persons on f.id equals p.FamilyID
                        select new { f.street, p.FamilyID, p.IDnum, f.Husbandid, f.wifeid, f.cellnumber, f.Telephone, p.LastName, f.HomeNumber };


            }

            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textStreet.Text))
            {
                // search by both;
                query = from f in db.FamilyPersons.Where(x => x.street.StartsWith(textStreet.Text))
                        join p in db.Persons.Where(x => (x.LastName.StartsWith(textBox1.Text))) on f.id equals p.FamilyID
                        select new { f.street, p.FamilyID, p.IDnum, f.Husbandid, f.wifeid, f.cellnumber, f.Telephone, p.LastName, f.HomeNumber };              
               ;

                
            }


            var query2 = from a in query
                         group a by a.FamilyID into newfamily
                         select new
                         {
                             PostalCode = newfamily.Key,
                             newfamily = newfamily
                         };


             foreach (var item1 in query2)
                {
                    foreach (var item in item1.newfamily)
                    {
                        FamilyPerson currentFamilyPerson = db.FamilyPersons.FirstOrDefault(x => (x.Husbandid == item.IDnum) || (x.wifeid == item.IDnum));
                        FamilyPhoneBook _familyDetails = new FamilyPhoneBook(currentFamilyPerson.id);
                        // _familyDetails.date = Convert.ToDateTime(item.date).ToString("M/d/yyyy");
                        //   _familyDetails.Amount = item.amount;
                        _familyDetails.Titleamount = "مبلغ الترميم";
                        //_familyDetails.AdditionalData += "عدد الغرف :" + item.RoomNumbers + "\n";
                        //_familyDetails.AdditionalData += " احتياجات الترميم :" + item.Tarmimdemands + "\n";
                        //_familyDetails.AdditionalData += "  تكاليف الترميم :" + item.TarmimTakalef + "\n";
                        //_familyDetails.AdditionalData += "   ملاحظات :" + item.Note + "\n";
                        // _personinproject.ProjectName = comboBox1.Text;
                        allFamiliesinProj.Add(_familyDetails);
                        break;
                    }
                 
                }
          


            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet_PersoninFamily", allFamiliesinProj));
            //ReportParameter p1 = new ReportParameter("DateFrom", dateFrom.Value.ToString("mm/dd/yyyy"));
            //ReportParameter p2 = new ReportParameter("DateTo", dateTo.Value.ToString("mm/dd/yyyy"));
            //ReportParameter p3 = new ReportParameter("project", comboBox1.Text);
            //ReportParameter p4 = new ReportParameter("MasrofSummary", MasrofSummary);

            //reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1, p2, p3, p4 });
            reportViewer1.Visible = true;
            this.reportViewer1.RefreshReport(); 


           
          
            
        }
        /// <summary>
        /// Render Report of selected PRoject
        /// </summary>
        /// <param name="p"></param>
        private void RenderReport()
        {
          
        }

          

           private void ReportByDateAndProject_Load(object sender, EventArgs e)
           {

               this.reportViewer1.RefreshReport();
           }
    

           private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
           {
               btnAddFamily.Enabled = true;
           }

        }

   
    }



